import { DetailDataList, addShoppingCart } from '@/api/detail';
export default {
	// 开启命名空间
	namespaced: true,
	state: {
		detailDataList: {},
	},
	mutations: {
		detailDataObj(state, obj) {
			state.detailDataList = obj;
		},
	},
	actions: {
		async getDetailList(context, num) {
			let result = await DetailDataList(num);
			if (result.data.code === 200) {
				context.commit('detailDataObj', result.data.data);
			}
		},
		async addCart(context, { skuId, skuNum }) {
			let result = await addShoppingCart(skuId, skuNum);
			// 判断数据返回是否成功
			if (result.data.code === 200) {
				return 'true';
			} else {
				return Promise.reject(new Error('Adding cart failed, please try again'));
			}
		},
	},
	getters: {
		categoryView(state) {
			return state.detailDataList.categoryView || {};
		},
		skuInfo(state) {
			return state.detailDataList.skuInfo || {};
		},
		spuSaleAttrList(state) {
			return state.detailDataList.spuSaleAttrList || [];
		},
	},
};
